package pt.ua.helpdesk.client;

import java.util.ArrayList;


import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

public class PerformanceAssList extends Composite {

	public PerformanceAssList() {

		final HorizontalPanel horizontalPanel = new HorizontalPanel();
		initWidget(horizontalPanel);

		final VerticalPanel verticalPanel = new VerticalPanel();
		horizontalPanel.add(verticalPanel);
		System.out.println("Entrou no composite certo");
		ServicePerformance.Util.getInstance().getAssistants(new ListAssistantsAsyncCallback(verticalPanel));

	}
	static class ListAssistantsAsyncCallback implements AsyncCallback 
	{
		  VerticalPanel vp;
		  
		  public ListAssistantsAsyncCallback(VerticalPanel vp) {
			  this.vp= vp;
		  }
		  
		  /**
		   * Success handler
		   */
		  public void onSuccess(Object object) {
			  ArrayList Assistants = (ArrayList) object;
			  ArrayList idAssis = (ArrayList)Assistants.get(0);
			  ArrayList names = (ArrayList)Assistants.get(1);
			  			  
			  Grid grid = new Grid();
			  
			  
			  grid.resize(idAssis.size()+1, 3);
			  grid.setStyleName("grid");
			  grid.setBorderWidth(1);
			  grid.setCellSpacing(4);
			  			  
			  Label lbl1 = new Label();
			  lbl1.setText("#");
			  lbl1.setStyleName("section_title");
			  grid.setWidget(0, 0, lbl1);
			  
			  Label lbl2 = new Label();
			  lbl2.setText("Assistente");
			  lbl2.setStyleName("section_title");
			  grid.setWidget(0, 1, lbl2);
			  		  
			  //ver
			  Label lbl4 = new Label();
			  lbl4.setText("");
			  lbl4.setStyleName("section_title");
			  grid.setWidget(0, 2, lbl4);
			  
			  for(int i = 0; i < idAssis.size(); i++)
			  {		  
				  Label lblCount = new Label();
				  lblCount.setText(""+(i+1));
				  lblCount.setStyleName("label");
				  grid.setWidget(i+1, 0, lblCount);
				  
				  Label lblName = new Label();
				  lblName.setText(names.get(i).toString());
				  lblName.setStyleName("label");
				  grid.setWidget(i+1, 1, lblName);
				  		  		  			  
				  Button btnVer = new Button();
				  btnVer.setText("Ver");
				  btnVer.setStyleName("button");
				  btnVer.addClickListener(new PerformanceAssOnClick((Integer)idAssis.get(i), names.get(i).toString()));
				  grid.setWidget(i+1, 2, btnVer); 
			  }
			  
			  vp.add(grid);
			     	
		  }
		  
		  /**
		   * Failure handler
		   */
		  public void onFailure(Throwable throwable) {
			  System.out.println("FALHOU!");
		  }
		  
		}

}
